<template>
  <el-row v-loading="loading">
    <h1>人群分类</h1>
    <el-col :span="24">
      <pop-edit ref="popEdit" v-on:save-finished="searchPop"></pop-edit>
      <el-row class="search-row">
        <el-col :span="12">
          <el-input
            placeholder="档案号/姓名"
            size="small"
            v-model="search.name"
          >
            <el-button slot="append" icon="el-icon-search" @click="searchPop()"
              >查询</el-button
            >
          </el-input>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-table
            :data="populationList"
            border
            size="mini"
            header-cell-class-name="header-row"
          >
            <el-table-column
              prop="fileNumber"
              label="档案号"
              header-align="center"
              align="center"
            ></el-table-column>
            <el-table-column
              prop="name"
              label="姓名"
              header-align="center"
            ></el-table-column>
            <el-table-column
              prop="sex"
              label="性别"
              header-align="center"
            ></el-table-column>
            <el-table-column
              prop="age"
              label="年龄"
              header-align="center"
            ></el-table-column>
            <el-table-column
              prop="popStart"
              label="人群分类"
              header-align="center"
            ></el-table-column>
            <el-table-column
              prop="healthManager"
              label="健康管理师"
              header-align="center"
            ></el-table-column>
            <el-table-column
              label="操作"
              header-align="center"
              align="center"
              width="260"
            >
              <template slot-scope="scope">
                <el-button
                  v-show="permission.edit"
                  type="primary" round
                  size="mini"
                  @click="handleChangePassword(scope.$index, scope.row)"
                  >确认分类</el-button
                >
                <!-- 删除 -->
                <el-button
                  v-show="permission.remove"
                  type="danger" icon="el-icon-delete" circle
                  size="mini"
                  @click="handleDeletePopulation(scope.$index, scope.row)"
                  ></el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="1"
            :page-sizes="[3, 10, 20]"
            :page-size="3"
            layout="total, sizes, prev, pager, next, jumper"
            :total="tableTotal"
          >
          </el-pagination>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>



<script>
import PopEdit from "@/components/HealthIntervention/populationClassification/popEdit.vue";
import {
  findPopualtion,
  removePopualtion,
  getPopualtion,
} from "@/api/HealthIntervention/populationClassification";
import { ListAllDisease } from "@/api/HealthIntervention/disease";

import { listAllRole } from '@/api/role'

export default {
  data() {
    return {
      populationList: [],
      
      permission: {
        add: true,
        edit: true,
        remove: true,
      },
      tableTotal: 0,
      search: {
        name: "",
        offset: 1,
        limit: 3,
      },
      loading: true,
    };
  },
  methods: {
    searchPop() {
      this.loading = true;
      findPopualtion(this.search)
        .then((response) => {
          this.populationList = response.data.rows;
          this.tableTotal = response.data.total;
          this.loading = false;
        })
        .catch((error) => {
          this.outputError(error);
        });
    },

    //分页
    handleSizeChange(val) {
      this.search.limit = val;
      this.search.offset = 1;
      this.searchPop();
    },
    handleCurrentChange(val) {
      this.search.offset = val;
      this.search.currentPage = val;
      this.searchPop();
    },

    async handleChangePassword(index, row) {
      this.loading = true;
      try {
        let [ListPopualtion, allRoleResp] = await Promise.all([
          getPopualtion(row.id),
          ListAllDisease(),
        ]);
        this.$refs.popEdit.$emit(
          "openEditPopDialog",
          ListPopualtion.data,
          allRoleResp.data
        );
        this.loading = false;
      } catch (error) {
        this.outputError(error);
      }
    },

    //删除
    handleDeletePopulation(index, row) {
      this.$confirm("确定删除吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.loading = true
          removePopualtion(row.id)
            .then((response) => {
              this.searchPop();
              this.$message({
                message: "已删除",
                type: "success",
              });
            })
            .catch((error) => {
              this.outputError(error);
            });
        })
        .catch(() => {});
    },

    async pageInit() {
      this.loading = true;
      try {
        let [searchPopResp, getPermissionResp] = await Promise.all([
          findPopualtion(this.search),
        ]);
        this.populationList = searchPopResp.data.rows;
        this.tableTotal = searchPopResp.data.total;
        this.loading = false;
      } catch (error) {
        this.outputError(error);
      }
    },
    outputError(error) {
      console.log(error.response ? error.response : error.message);
      this.loading = false;
      this.$message({
        showClose: true,
        message: "出错了，请按F12查看浏览器日志。",
        type: "error",
      });
    },
  },
  components: { PopEdit },
  created() {
    this.pageInit();
  },
};
</script>

<style lang="scss" scoped>
.search-row {
  padding-bottom: 8px;
  .el-input {
    width: 260px;
  }
}

.user-locked {
  color: red;
}
.user-not-locked {
  color: green;
}
</style>

